package com.sls.nzdapplication.db.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.sls.nzdapplication.db.DaoManager;
import com.sls.nzdapplication.db.DaoSession;
import com.sls.nzdapplication.db.SwitchEquipmentDao;
import com.sls.nzdapplication.model.SwitchEquipment;
import com.sls.nzdapplication.app.MySharedPreferences;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SwitchEquipmentUtil {

    private static final boolean DUBUG = true;
    private DaoManager manager;
    private SwitchEquipment switchEquipment;
    private DaoSession daoSession;
    private MySharedPreferences mySharedPreferences;
    private String username = "";

    public SwitchEquipmentUtil(Context context){
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
        mySharedPreferences = new MySharedPreferences(context);
        username = mySharedPreferences.getUserValue("username");
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertSwitchEquipment(SwitchEquipment switchEquipment) {
        switchEquipment.setCreateBy(username);
        switchEquipment.setCreateTime(new Date());
        manager.getDaoSession().insertOrReplace(switchEquipment);
    }


    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultSwitchEquipment(final List<SwitchEquipment> switchEquipments) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (SwitchEquipment switchEquipment : switchEquipments) {
                    switchEquipment.setCreateBy(username);
                    switchEquipment.setCreateTime(new Date());
                    manager.getDaoSession().insertOrReplace(switchEquipment);
                }
            }
        });
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultSwitchEquipment2(final List<SwitchEquipment> switchEquipments) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (SwitchEquipment switchEquipment : switchEquipments) {
                    manager.getDaoSession().insertOrReplace(switchEquipment);
                }
            }
        });
    }


    public List<SwitchEquipment>  querySwitchEquipmentBySid(String id){
        QueryBuilder<SwitchEquipment> builder = manager.getDaoSession().queryBuilder(SwitchEquipment.class);
        List<SwitchEquipment> switchEquipments = builder.where(SwitchEquipmentDao.Properties.Sid.eq(id)).list();
        return  switchEquipments;
    }

    public List<SwitchEquipment> querySwitchEquipmentByEid(String id){
        QueryBuilder<SwitchEquipment> builder = manager.getDaoSession().queryBuilder(SwitchEquipment.class);
        List<SwitchEquipment> switchEquipments = builder.where(SwitchEquipmentDao.Properties.Eid.eq(id)).list();
        return  switchEquipments;
    }


    /**
     * 删除数据
     */
    public void deleteSwitchEquipment(SwitchEquipment switchEquipment) {
        manager.getDaoSession().delete(switchEquipment);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 更新数据
     */
    public void updateSwitchEquipment(SwitchEquipment switchEquipment) {
        switchEquipment.setModifyBy(username);
        switchEquipment.setModifyTime(new Date());
        manager.getDaoSession().update(switchEquipment);
    }

    /**
     * 按照主键返回单条数据
     */
    public SwitchEquipment listOneSwitchEquipment(String key) {
        return manager.getDaoSession().load(SwitchEquipment.class, key);
    }

    public List<SwitchEquipment> queryAll() {
        return manager.getDaoSession().loadAll(SwitchEquipment.class);
    }

    public JSONArray queryAllToJson(){
        JSONArray jsonArray = new JSONArray();
        List<SwitchEquipment> Switchs = queryAll();
        for(SwitchEquipment mySwitch :Switchs){
            jsonArray.add(mySwitch.toJson());
        }
        return jsonArray;
    }

    public void insertByJsArray(JSONArray jsonArray){
        List<SwitchEquipment> list = new ArrayList<>();
        for(int i = 0;i<jsonArray.size();i++){
            list.add(new SwitchEquipment(jsonArray.getJSONObject(i)));
        }
        insertMultSwitchEquipment2(list);
    }

}
